################################################################################
# Makefile
#
# The Makefile needs to be complete at the TODO flags.
# The UUID is obtained by using the command line tool named uuidgen.
# The dpkg-deb and ldid tools are required (edit the dpkg_deb_BIN and ldid_BIN
# variables to link those tools).
#
# Adrien Bougouin <adrien.bougouin@gmail.com>
################################################################################

# TODO ################
# fill the below fields
exec_NAME       = comicsblog-fr
project_NAME    = ComicsBlog
project_ID			= comicsblog
project_VERSION = 1.0
UUID            = BE9E7216-322C-4867-81D8-50EE187F9906
device_IP       = 169.254.13.37
simulation      = no

################################################################################
# System configurations

darwin_VERSION  = 10
gcc_VERSION     = 4.2.1
sdk_VERSION     = 4.3
platforms_DIR   = /Developer/Platforms
iphone_NAME     = iPhoneOS
simulator_NAME  = iPhoneSimulator
iphone_ARCH     = arm
simulator_ARCH  = i686
simulator_LIB   = ~/Library/"Application Support"/"iPhone Simulator"

iphone_PLATFORM     = $(platforms_DIR)/$(iphone_NAME).platform
simulator_PLATFORM  = $(platforms_DIR)/$(simulator_NAME).platform

ldid_BIN = /opt/cydia_tools/ldid
dpkg_deb_BIN = /opt/cydia_tools/dpkg-deb

################################################################################
# Device type

ifeq ($(simulation), yes)
platform_NAME = $(simulator_NAME)
device_ARCH   = $(simulator_ARCH)
else
platform_NAME = $(iphone_NAME)
device_ARCH   = $(iphone_ARCH)
endif

device_PLATFORM = $(platforms_DIR)/$(platform_NAME).platform

################################################################################
# Building configurations
# TODO ensure that all the sources are builded

app_NAME      = $(project_NAME).app
info_PLIST    = $(platform_NAME).plist
ipa_NAME      = $(project_NAME).ipa
deb_NAME      = $(project_NAME).deb
deb_INFO      = package_info
source_DIR    = src
source_FILES  = $(wildcard $(source_DIR)/*.m) \
                $(wildcard $(source_DIR)/model/*.m) \
                $(wildcard $(source_DIR)/view/*.m) \
                $(wildcard $(source_DIR)/view_controller/*.m)
header_FILES  = $(wildcard $(source_DIR)/*.h) \
                $(wildcard $(source_DIR)/model/*.h) \
                $(wildcard $(source_DIR)/view/*.h) \
                $(wildcard $(source_DIR)/view_controller/*.h)
resource_DIR  =	resources
build_DIR     = build
device_BUILD  = $(build_DIR)/$(sdk_VERSION)/$(platform_NAME)
obj_DIR       = $(device_BUILD)/.obj
obj_FILES     = $(patsubst %.m,$(obj_DIR)/%.o,$(source_FILES))
sed_CMD       = -e 's/exec_NAME/$(exec_NAME)/g' \
                -e 's/app_NAME/$(project_NAME)/g' \
                -e 's/project_VERSION/$(project_VERSION)/g' \
                -e 's/project_ID/$(project_ID)/g'

gcc_BIN       = $(device_ARCH)-apple-darwin$(darwin_VERSION)-gcc-$(gcc_VERSION)

CC  = $(device_PLATFORM)/Developer/usr/bin/$(gcc_BIN)
LD  = $(CC)
SDK = $(device_PLATFORM)/Developer/SDKs/$(platform_NAME)$(sdk_VERSION).sdk

CFLAGS += -isysroot $(SDK)
CFLAGS += -x objective-c
CFLAGS += -std=c99
CFLAGS += -fnested-functions
ifeq ($(simulation), yes)
CFLAGS += -fobjc-abi-version=2
CFLAGS += -fobjc-legacy-dispatch
CFLAGS += -D__SIMULATION__
endif
CFLAGS += -D__IPHONE_OS_VERSION_MIN_REQUIRED=40100

LDFLAGS += -isysroot $(SDK)
LDFLAGS += -framework CoreFoundation 
LDFLAGS += -framework CoreGraphics 
LDFLAGS += -framework Foundation 
LDFLAGS += -framework UIKit

################################################################################
# Creation

.PHONY: init app dist install uninstall uninstall_and_wait clean cleanapp cleandist mrproper

####################
# default
default: app

####################
# init
# TODO ensure that all the build path is created
init:
	@export COPYFILE_DISABLE
	@export COPY_EXTENDED_ATTRIBUTES_DISABLE
	@mkdir -p $(device_BUILD)
	@mkdir -p $(obj_DIR)/$(source_DIR)/model
	@mkdir -p $(obj_DIR)/$(source_DIR)/view
	@mkdir -p $(obj_DIR)/$(source_DIR)/view_controller

####################
# app
app: init $(device_BUILD)/$(app_NAME)

$(device_BUILD)/$(app_NAME): $(device_BUILD)/$(exec_NAME) $(info_PLIST)
	@echo ">> Creating $(app_NAME)..."
	@mkdir -p $(device_BUILD)/$(app_NAME)
	@cp -R $(resource_DIR)/* $(device_BUILD)/$(app_NAME)
	@sed $(sed_CMD) $(info_PLIST) > $(device_BUILD)/$(app_NAME)/Info.plist
	@echo "APPL????" > $(device_BUILD)/$(app_NAME)/PkgInfo
	@cp $(device_BUILD)/$(exec_NAME) $(device_BUILD)/$(app_NAME)
ifeq ($(simulation), no)
	@chmod 755 $(device_BUILD)/$(app_NAME)/$(exec_NAME)
	@$(ldid_BIN) -S $(device_BUILD)/$(app_NAME)/$(exec_NAME)
endif

$(device_BUILD)/$(exec_NAME): $(obj_FILES)
	@echo ">> Compiling $(exec_NAME)"
	@$(LD) $(LDFLAGS) -o $@ $^

$(obj_DIR)/%.o: %.m $(header_FILES)
	@echo ">> $<\n   |-> $@"
	@$(CC) $(CFLAGS) -o $@ -c $<

####################
# dist
dist: app $(device_BUILD)/$(ipa_NAME) $(device_BUILD)/$(deb_NAME)

$(device_BUILD)/$(ipa_NAME):
	@echo ">> Creating $(ipa_NAME)..."
	@mkdir Payload
	@cp -R $(device_BUILD)/$(app_NAME) Payload
	@zip -r $(device_BUILD)/$(ipa_NAME) Payload
	@rm -rf Payload

$(device_BUILD)/$(deb_NAME):
	@echo ">> Creating $(deb_NAME)..."
	@mkdir -p $(device_BUILD)/$(project_NAME)/Applications
	@mkdir -p $(device_BUILD)/$(project_NAME)/DEBIAN
	@cp -R $(device_BUILD)/$(app_NAME) $(device_BUILD)/$(project_NAME)/Applications
	@sed $(sed_CMD) $(deb_INFO) > $(device_BUILD)/$(project_NAME)/DEBIAN/control
	@$(dpkg_deb_BIN) -b $(device_BUILD)/$(project_NAME)
	@rm -rf $(device_BUILD)/$(project_NAME)

####################
# install
install: dist
	@echo ">> Installing $(app_NAME)..."
ifeq ($(simulation), yes)
	@mkdir -p $(simulator_LIB)/$(sdk_VERSION)/Applications/$(UUID)/
	@cp -R $(device_BUILD)/$(app_NAME) $(simulator_LIB)/$(sdk_VERSION)/Applications/$(UUID)/
else
	@scp -r $(device_BUILD)/$(deb_NAME) root@$(device_IP):/
	@ssh root@$(device_IP) 'dpkg -i /$(deb_NAME); rm -rf /$(deb_NAME); reboot'
endif

####################
# uninstall
uninstall:
	@echo ">> Uninstalling $(app_NAME)..."
ifeq ($(simulation), yes)
	@rm -rf $(simulator_LIB)/$(sdk_VERSION)/Applications/$(UUID)/
else
	@ssh root@$(device_IP) 'dpkg -r $(project_ID); reboot'
endif

################################################################################
# Cleanup

clean:
	rm -rf $(obj_DIR) $(device_BUILD)/$(exec_NAME)

cleanapp:
	rm -rf $(device_BUILD)/$(app_NAME)

cleandist:
	rm -rf $(device_BUILD)/$(ipa_NAME)
	rm -rf $(device_BUILD)/$(deb_NAME)

mrproper:
	rm -rf $(build_DIR)

